import React, { useEffect, useRef, useState } from 'react'
import {
  Tabs,
  Input,
  List,
  Avatar,
  Row,
  Col,
  Space,
  AutoComplete,
  Divider,
  Select,
  Button,
  message
} from 'antd'
import {
  reqPatientList,
  reqDetal,
  reqUpdate,
  reqPulseUpdate,
  reqPulseDetail,
  reqPulseList,
  reqQuestionTermList
} from '@/services/basicConsultation';

const PulseInfo = ({
  user
}) => {
  const [data, setData] = useState<any>([])
  const isFirst = useRef(false)


  async function getPulseList(query: { tyPatientId: string, tyMedicalCaseId?: string }) {
    try {
      const res = await reqPulseList({
        pageNum: 1,
        pageSize: 10,
        ...query
        // tyPatientId: 5,
        // tyMedicalCaseId: 5
      })

      const detail = await reqPulseDetail({
        tyMedicalCaseId: query?.tyMedicalCaseId,
      })

      console.log('获取脉诊列表信息 :>> ', res?.data);
      console.log('获取脉诊信息详情 :>> ', detail?.data);
      if (detail?.resultCode == '00000000') {
        isFirst.current = true
        setData(detail?.data)
      } else {
        message.error(`获取脉诊列表信息出错！${res?.message}`)
      }
    } catch (error: any) {
      message.error('获取脉诊列表信息出错！', error)
      console.log('获取脉诊列表信息出错！ :>> ', error);
    }
  }


  useEffect(() => {
    if (user?.id && !isFirst.current) {
      getPulseList({
        tyPatientId: user?.tyPatientId,
        tyMedicalCaseId: user?.id
      })
    }

  }, [user, isFirst.current])

  const pulseDataJson = data && data?.pulseDataJson && JSON.parse(data?.pulseDataJson);
  console.log('pulseDataJson :>> ', pulseDataJson);
  if (!pulseDataJson) return <div>脉诊信息</div>;

  return (
    <>
    <div>
      脉诊信息
    </div>
    {!pulseDataJson?
    <Space direction="vertical" size={12}>
      <Space direction="vertical" size={12}>
        <div>
          <Space>
            <div>测量稳定指数：</div>
            <div>{pulseDataJson['stabilityIndex']}</div>
          </Space>
        </div>
        <div>
          <Space>
            <div>左手脉象：</div>
            <div>{pulseDataJson['measureTime']}</div>
          </Space>
        </div>
        <div>
          <Space>
            <div>心率：</div>
            <div>{pulseDataJson['heartRate']}</div>
          </Space>
        </div>
        <div>{pulseDataJson['branchPulse']?.name}</div>
      </Space>
      <div>
        <Space direction='vertical' size={12}>
          <div>
            <Space direction='vertical' size={12}>
              {pulseDataJson['branchPulse']?.value.map((item: { name: string, value: string }, index: number) => (
                <div key={index}>
                  <Space size={8}>
                    <div>{item?.name}：</div>
                    <div>{item?.value}</div>
                  </Space>
                </div>
              ))}
            </Space>
          </div>
          <div>
            <Space direction='vertical' size={12}>
              {pulseDataJson['handPulse']?.map((item: { name: string, value: string }, index: number) => (
                <div key={index}>
                  <Space size={8}>
                    <div>{item?.name}：</div>
                    <div>{item?.value}</div>
                  </Space>
                </div>
              ))}
            </Space>
          </div>
        </Space>
      </div>
    </Space>
    :null}
    </>
  )
}

export default PulseInfo;